/*
 * 流程实例管理 2010-01-15
 * author : tss
 * 
 */

//url 路径前缀
var startUrl = "/workflow/processInst/";
var ProcessInst = {

	id : "ProcessInst",
	enterNewUrl : startUrl+"enterNew.html",
	enterEditUrl : startUrl+"enterEdit.html",
	enterViewUrl : startUrl+"enterView.html",
	saveUrl : startUrl + "start.html",
	pauseUrl : startUrl + "pause.html",
	resumeUrl : startUrl + "resume.html",
	endUrl : startUrl + "end.html",
	removeUrl : startUrl + "delete.html",
	queryUrl : startUrl + "list.html",
	chkIds : "chkIds",
	form : function(){
	  return document.forms[0];
    },

	// 校验
	validate : function() {
		/*try {
			return Validator.Validate(ProcessInst.form(), 3);
		} catch (e) {
			alert(e.message);
		}
		return false;*/
    	return true;
	},

	//首页
	desktop : function () {
	   
	  doSubmit(ProcessInst.form(),initVar.desktopUrl);
	},
	
	// 查询
	search : function search() {
		doSubmit(ProcessInst.form(), ProcessInst.queryUrl);
	},
	
	// 高级查询
	adSearch : function search() {
		queryUpDown();
	},

	//进入新增
	enterNew : function() {

		var id = getSelectFirstId(ProcessInst.chkIds);
		doSubmit(ProcessInst.form(),ProcessInst.enterNewUrl+'?id='+id);
	},
	
	//进入修改
	enterEdit : function(id) {
		
		doSubmit(ProcessInst.form(), ProcessInst.enterEditUrl+'?id='+id);
	},

	// 进入详细
	enterView : function(id) {

		doSubmit(ProcessInst.form(), ProcessInst.enterViewUrl + "?id=" + id);
	},

	// 删除
	remove : function() {

		commonRemove(ProcessInst.form(), ProcessInst.removeUrl, ProcessInst.chkIds);
	},

	//发起
	start : function() {

		doSubmit(ProcessInst.form(), ProcessInst.saveUrl);
	},
	
	//暂停
	pause : function(id) {
		
		doSubmit(ProcessInst.form(), ProcessInst.pauseUrl + "?id=" + id);
	},
	
	//恢复
	resume : function(id) {
		
		doSubmit(ProcessInst.form(), ProcessInst.resumeUrl + "?id=" + id);
	},
	
	//结束
	end : function(id) {
		
		doSubmit(ProcessInst.form(), ProcessInst.endUrl + "?id=" + id);
	},


	// 返回
	goBack : function() {
		
		doSubmit(ProcessInst.form(), ProcessInst.queryUrl+"?redirect=true"); 
	}
};

var varDyTable;

//加载
window.onload = function() {

	setTbStyle();
	execEvent(ProcessInst,"INPUT","button");
	execEvent(ProcessInst,"A");
	
	keyToSearch("search");
	
	if(ProcessInst.form().getAttribute("operate") != "newEditForm"){
		return;
	}
	
	//流程变量动态表格初始化
	var getChkStr = function(op){
		return "<input type=\"checkbox\" name=\"chk_"+op+"_id\" value=\"$!{entity.id}\"/>";

	};
	var getKey = function(op){
		return "<input type=\"text\" name=\"varKey\"/>";
		
	};
	var getValue = function(op){
		return "<input type=\"text\" name=\"varValue\"/>";
		
	};
	
	varDyTable = new DyTable("tbVarMap","varDyTable");
	varDyTable.config.trClsName = "tb_c";
	var pop = "varMap";
	varDyTable.config.tdHtml = [getChkStr(pop),getKey(pop),getValue(pop)];
	varDyTable.config.chkIds = "chk_varMap_id";
	varDyTable.config.autoDelOp = true;
	
	//事件注册
	varDyTable.registerEvent(varDyTable.eventType.add, "var_save");
	varDyTable.registerEvent(varDyTable.eventType.delAll, "var_del");
	
	//初始化数据
	var propertyList = eval(document.getElementById("propertyList"));
	if(propertyList != null){
		for(var i = 0; i < propertyList.length; i++){
			var property = propertyList[i];
			varDyTable.config.tdHtmlValue = ["select|"+property.accessRights.id,"checkbox|"+(property.state != null ? property.state.id : ""),
			                               "text|"+property.javaType,"text|"+property.name,"textarea|"+property.comments];
			varDyTable.addFullRow();
		}
	}
};
